<?php
/**
 * Url class
 * 
 * @author Mathieu Pheulpin - sokaru91@gmail.com
 *
 */
class Mac_Url
{
    protected $module;
    protected $controller;
    protected $action;
    protected $ordered_params = array();
    protected $unordered_params = array();
    
    public function __construct ($module = null, $controller = null, $action = null, $op = array(), $up = array())
    {
        $this->module = $module;
        $this->controller = $controller;
        $this->action = $action;
        $this->ordered_params = $op;
        $this->unordered_params = $up;
    }
    
    public function setModule ($module)
    {
        $this->module = $module;
    }
    
    public function setController ($controller)
    {
        $this->controller = $controller;
    }
    
    public function setAction ($action)
    {
        $this->action = $action;
    }
    
    public function setOrderedParams ($params)
    {
        $this->ordered_params = $params;
    }
    
    public function setUnorderedParams ($params)
    {
        $this->unordered_params = $params;
    }
    
    public function addUnorderedParam ($name, $value)
    {
        $this->unordered_params[$name] = $value;
    }
    
    public function __toString ()
    {
        $url = '/';
        if ($this->module !== null) {
            $url .= $this->module.'/';
        }
        if ($this->controller !== null) {
            $url .= $this->controller.'/';
        }
        if ($this->action !== null) {
            $url .= $this->action;
        }
        if (sizeof($this->ordered_params) > 0) {
            $url .= '/'.implode('/', $this->ordered_params);
        }
        if (sizeof($this->unordered_params) > 0) {
            $url .= '/-';
            foreach ($this->unordered_params as $param_name => $param_value) {
                $url .= $param_name.'/'.$param_value.'/';
            }
            $url = substr($url, 0, -1); // Remove last slash
        }
        return $url;
    }
    
    public static function create ($module = null, $controller = null, $action = null, $op = array(), $up = array())
    {
        return new self($module, $controller, $action, $op, $up);
    }
}